///@file StressVector.cpp
///@brief Implementation of the class representing the vector representation of a stress tensor
///@author Arnaud Duval
///@version 0.0
///@date 2011/03/16

#include "StressVector.h"
#include <cmath>

///@brief Overload of = operator for Vector
///@param v Vector object righthand operand
StressVector& StressVector::operator= (const Vector &v)
{
    if(size)
      delete[] a;
    size = v.Dim();
    a = new double[size];
    for(unsigned int i = 0 ; i < size ; i++)
        a[i] = v[i+1];
    return (*this);
}

///@brief Overload of + operator
///@param v StressVector right hand operand
StressVector StressVector::operator+ (const StressVector &v)
{
    assert(size == v.size);
    StressVector res(size);
    for(unsigned int i = 0 ; i < size ; i++)
        res.a[i] = a[i] + v.a[i];
    return res;
}

///@brief Overload of - operator
///@param v stressVactor right hand operand
StressVector StressVector::operator- (const StressVector &v)
{
    assert(size == v.size);
    StressVector res(size);
    for(unsigned int i = 0 ; i < size ; i++)
        res.a[i] = a[i] - v.a[i];
    return res;
}

///@brief Overload of * operator
///@param v StressVector right hand operand
Matrix StressVector::operator* (const StressVector &v)
{
    assert(size == v.size);
    Matrix res(size, size);
    for(unsigned int i = 0 ; i < size ; i++)
        for(unsigned int j = 0 ; j < size ; j++)
            res(i+1,j+1) = a[i]*v.a[j];
    return res;
}

///@brief Compute Von Mises equivalent stress
///@bug Works only for Vcetor with size = 3
double StressVector::VMEquiv()
{
    assert(size == 3);
    return sqrt((3./2.)*(a[0]*a[0] + a[1]*a[1] - a[0]*a[1] + 3.*a[2]*a[2]));
    
}